package cn.cmcc.online.smsapi.ui.presenter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;

import java.util.List;

import cn.cmcc.online.smsapi.ui.manager.IDataCallback;
import cn.cmcc.online.smsapi.ui.model.MetaData;
import cn.cmcc.online.smsapi.ui.model.TableData;
import cn.cmcc.online.smsapi.ui.model.TrainData;
import cn.cmcc.online.smsapi.ui.model.TransportSelectInfo;
import cn.cmcc.online.smsapi.ui.util.DataExchangeProxy;
import cn.cmcc.online.smsapi.ui.util.DataUtil;

public class TrainPresenterImpl extends TrainPresenter {
    private Context mContext = null;
    private int mCurrentTrainIndex = 0;
    private int mCurrentStationIndex = 0;
    private TrainData mCurrentTrainData = null;
    private MetaData mMetaData = null;
    private IDataCallback<JSONObject> mTrainInfoListener = null;

    public TrainPresenterImpl(Context context) {
        mContext = context;
        mTrainInfoListener = new IDataCallback<JSONObject>() {
            @Override
            public void onReady(int status, JSONObject data) {
                mCurrentTrainData.updateWithWebData(data);
                DataUtil.updateJSONObject(cardData, mCurrentTrainData);
                showBodyAndUpdate();
            }
        };
    }

    private void showBody() {
        mCurrentTrainData = DataUtil.getTrainData(cardData, mCurrentTrainIndex);
        if (mCurrentTrainData != null) {
            vista.showBody(mCurrentTrainData, mMetaData);
        }
    }

    private void showBodyAndUpdate() {
        showBody();
        vista.updateBottom();
        DataExchangeProxy.getInstance().updateCardInfo(cardData);
    }

    @Override
    public void bind(JSONObject data) {
        super.bind(data);
        mMetaData = DataUtil.getMetaData(cardData);

        showBody();
        if (DataUtil.getMainData(cardData).length() > 1) {
            vista.enableTrainSelect(mMetaData);
        } else {
            vista.disableTrainSelect();
        }

        List<TableData> dataList = DataUtil.getTableList(cardData);
        if (dataList != null && dataList.size() > 0) {
            vista.showTable(dataList);
        }

        String tips = DataUtil.getNote(cardData);
        if (!TextUtils.isEmpty(tips) && !tips.equals("null")) {
            vista.showNote(tips);
        }
    }

    @Override
    public void doAction() {
        DataExchangeProxy.getInstance().setTrainInfoListener(mTrainInfoListener);
    }

    @Override
    public void selectTrain(int index) {
        mCurrentTrainIndex = index;
        showBodyAndUpdate();
    }

    @Override
    public void selectStation(int index) {
        mCurrentStationIndex = index;
        showBodyAndUpdate();
    }

    @Override
    public void queryTrainNumber() {
        vista.showTrainSelect(new TransportSelectInfo(cardData, TransportSelectInfo.TRANSPORT_TRAIN), mCurrentTrainIndex);
    }

    @Override
    public void queryTrainStation() {
        if (mCurrentTrainData != null) {
            mCurrentTrainData.setReceiveTime(DataUtil.getReceiveTime(cardData));
            DataExchangeProxy.getInstance().setTrainInfoListener(mTrainInfoListener);
            DataExchangeProxy.getInstance().getTrainInfo(mCurrentTrainData);
        }
    }
}
